package cn.yeziji.forum.controller;

import cn.yeziji.forum.common.ForumResult;
import cn.yeziji.forum.dao.intf.ForumUserDao;
import cn.yeziji.forum.dto.user.EmailDTO;
import cn.yeziji.forum.dto.user.UserFocusOrFansListPagingDTO;
import cn.yeziji.forum.dto.user.UserRegisterDTO;
import cn.yeziji.forum.dto.user.UserUpdateDTO;
import cn.yeziji.forum.utils.ForumResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制器
 *
 * @author gzkemays
 * @since 2022/3/17
 */
@RestController
@RequestMapping("/user")
public class ForumUserController {
  @Resource ForumUserDao userDao;

  @PostMapping("/update")
  public ForumResult update(@Valid UserUpdateDTO dto) {
    return ForumResultUtils.ok(userDao.update(dto));
  }

  @PostMapping("/code")
  public ForumResult code(@Valid EmailDTO email) {
    userDao.registerSendEmail(email);
    return ForumResultUtils.ok("OK");
  }

  @PostMapping("/register")
  public ForumResult register(@Valid @RequestBody UserRegisterDTO dto) {
    userDao.register(dto);
    return ForumResultUtils.ok("注册成功");
  }

  @PostMapping("/msg")
  public ForumResult getUserMsg(String nickname) {
    return ForumResultUtils.ok(userDao.getUserMsg(nickname));
  }

  @PostMapping("/focusOrFans")
  public ForumResult getFocusOrFans(@RequestBody UserFocusOrFansListPagingDTO pagingDTO) {
    return ForumResultUtils.ok(userDao.getFocusOrFansUserMsg(pagingDTO));
  }

  @PostMapping("/focusAndFans")
  public ForumResult getFocusAndFansUserMsg(@RequestBody UserFocusOrFansListPagingDTO pagingDTO) {
    return ForumResultUtils.ok(userDao.getFocusAndFansUserMsg(pagingDTO));
  }

  @PostMapping("/msgs")
  public ForumResult getUserMsgList(@RequestBody List<String> nickname) {
    return ForumResultUtils.ok(userDao.getUserMsgList(nickname));
  }

  /**
   * @deprecated {@link #getFocusOrFans} 接口来根据条件获取，并支持分页
   */
  @GetMapping("/focus")
  public ForumResult getFocusAndFans() {
    return ForumResultUtils.ok(userDao.getFocusAndFansUserMsg());
  }

  @GetMapping("/count")
  public ForumResult count() {
    return ForumResultUtils.ok(userDao.getUserCount());
  }
}
